package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.biz.transfer.ITransferDzApplyDiviService;
import com.zjdiepu.www.proxy.dao.transfer.TransferDzApplyDiviMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferDzApplyDivi;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferDzApplyDiviExt;

/**
 * 中转_dz_申请分期Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 17时50分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferDzApplyDiviServiceImpl extends BaseService implements ITransferDzApplyDiviService {
	public static final Logger logger = LoggerFactory.getLogger(TransferDzApplyDiviServiceImpl.class);

	@Autowired
	private TransferDzApplyDiviMapper transferDzApplyDiviMapper;
	
	@Override
	@Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRES_NEW,readOnly=false,timeout=60)
	public void updateJust(TransferDzApplyDivi transferDzApplyDivi) {
		transferDzApplyDivi.initUpdateData(transferDzApplyDivi.getModifier(), transferDzApplyDivi.getModifierName());
		// 影响的记录数
		int updateCount = transferDzApplyDiviMapper.updateByPrimaryKeySelective(transferDzApplyDivi);
		if(updateCount == 0){
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		} else if(updateCount > 1){
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		}
	}
	
	@Override
	public void saveUpdateAndDelete(TransferDzApplyDivi transferDzApplyDivi, OperateType operType) {
		switch (operType) {
		case add:
			transferDzApplyDivi.initInsertData(transferDzApplyDivi.getModifier(), transferDzApplyDivi.getModifierName());
			transferDzApplyDiviMapper.insert(transferDzApplyDivi);
			break;
		case edit:
			transferDzApplyDivi.initUpdateData(transferDzApplyDivi.getModifier(), transferDzApplyDivi.getModifierName());
			// 影响的记录数
			int updateCount = transferDzApplyDiviMapper.updateByPrimaryKeySelective(transferDzApplyDivi);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferDzApplyDivi.initDeleteData(transferDzApplyDivi.getModifier(), transferDzApplyDivi.getModifierName());
			// 影响的记录数
			int deleteCount = transferDzApplyDiviMapper.updateByPrimaryKeySelective(transferDzApplyDivi);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferDzApplyDivi> selectByExt(TransferDzApplyDiviExt transferDzApplyDiviExt) {
		return transferDzApplyDiviMapper.selectByExt(transferDzApplyDiviExt);
	}
	
	@Override
	public int countByExt(TransferDzApplyDiviExt transferDzApplyDiviExt){
		return transferDzApplyDiviMapper.countByExt(transferDzApplyDiviExt);
	}
	
	@Override
	public TransferDzApplyDivi selectById(String id) {
		return transferDzApplyDiviMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferDzApplyDiviMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferDzApplyDivi transferDzApplyDivi) {
		return transferDzApplyDiviMapper.countRefined(transferDzApplyDivi);
	}

	@Override
	public TransferDzApplyDivi selectRefined(TransferDzApplyDivi transferDzApplyDivi) {
		return transferDzApplyDiviMapper.selectRefined(transferDzApplyDivi);
	}
	
	@Override
	public int deletedRefined(TransferDzApplyDivi transferDzApplyDivi) {
		return transferDzApplyDiviMapper.deletedRefined(transferDzApplyDivi);
	}

	@Override
	public boolean repeat(TransferDzApplyDivi transferDzApplyDivi) {
		int count = countRefined(transferDzApplyDivi);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferDzApplyDivi.getId())) {
			return true;
		}
		TransferDzApplyDivi dbItem = selectRefined(transferDzApplyDivi);
		if(null == dbItem) {
			return false;
		}
		return !transferDzApplyDivi.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferDzApplyDiviMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferDzApplyDivi> transferDzApplyDivis) {
		return transferDzApplyDiviMapper.insertBatch(transferDzApplyDivis);
	}
}
